package com.zhuliwei.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.zhuliwei.cloud.apis.PayFeignApi;
import com.zhuliwei.cloud.entities.PayDTO;
import com.zhuliwei.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * OpenFeign调用内部微服务cloud-provider-payment8001
 *
 * @author 朱力维
 * @time 2024/12/20 11:09
 **/
@RestController
public class OrderController {
    @Resource
    private PayFeignApi payFeignApi;
    /**
     * 一般情况下，通过浏览器的地址栏输入url，发送的只能是get请求
     * 我们底层调用的是post方法，模拟消费者发送get请求，客户端消费者
     * 参数可以不添加@RequestBody
     *
     * @param payDTO
     * @return
     */
    @GetMapping("/consumer/pay/add")
    public ResultData addOrder(PayDTO payDTO) {
        return payFeignApi.addPay(payDTO);
    }

    @GetMapping("/consumer/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id) {
        System.out.println("调用开始："+ DateUtil.now());
        ResultData payInfo = null;
        try{
            payInfo = payFeignApi.getPayInfo(id);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("调用结束："+ DateUtil.now());
            throw new RuntimeException();
        }
        return ResultData.success(payInfo);

    }

    // openfeign天然支持负载均衡功能
    @GetMapping(value = "/consumer/pay/get/info")
    public String getInfoByConsul() {
        return payFeignApi.mylb();
    }

}